package cn.lanqiao.jdk;

import cn.lanqiao.jdk.impl.TargetImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TargetProxyTest {
    public static void main(String[] args) {
        //代理的目标对象
        final TargetImpl target = new TargetImpl();
        final Advice advice = new Advice();
        //生成代理对象
        TargetInterface targetInterface = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                advice.before();
                //执行目标方法
                Object invoke = method.invoke(target,args);
                advice.after();
                return invoke;
            }
        });
        targetInterface.save();
    }
}
